<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../static/css/layui.css">
    <style>
        .layui-input {
            width: 400px;
        }
    </style>
</head>
<body>

<div class="layui-container">
    <form class="layui-form" action="" lay-filter="form_add" style="margin-top: 20px">
        <div class="layui-form-item">
            <label class="layui-form-label">学号</label>
            <div class="layui-input-block">
                <input type="text" name="s_id" lay-verify="required" autocomplete="off" placeholder="请输入"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-block">
                <input type="text" name="age" lay-verify="required" autocomplete="off" placeholder="请输入"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">java成绩</label>
            <div class="layui-input-block">
                <input type="text" name="_java" lay-verify="required" autocomplete="off" placeholder="请输入"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">python成绩</label>
            <div class="layui-input-block">
                <input type="text" name="_python" lay-verify="required" autocomplete="off" placeholder="请输入"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">mysql成绩</label>
            <div class="layui-input-block">
                <input type="text" name="_mysql" lay-verify="required" autocomplete="off" placeholder="请输入"
                       class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="add-commit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="../static/js/layui.js"></script>
<script>
    const add_student = async (data) => {
        const options = {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify(data)
        }
        const response = await fetch('/api/addStudent', options)
        return await response.json()
    }
    layui.use(function () {
        var $ = layui.$;
        var form = layui.form;

        // 提交事件
        form.on('submit(add-commit)', function (data) {
            var field = data.field; // 获取表单字段值
            console.log(field)
            // 此处可执行 Ajax 等操作


            add_student(field).then(function (res) {


                // 提交成功之后的回调
                if (!res.code) {
                    layer.msg(res.msg, {
                        icon: 1,
                        time: 1000,
                    }, function () {
                        parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                        parent.layui.table.reload('student');
                    });
                } else {
                    layer.msg(res.msg, {
                        icon: 2,
                        time: 1000,
                    });
                }
            })

            return false; // 阻止默认 form 跳转
        });
    })
</script>
</body>
</html>